#include "groupsessiondetailwidget.h"
#include <QIcon>
#include <QScrollArea>
#include <QScrollBar>
#include <QLabel>
#include <QMessageBox>

#include "sessiondetailwidget.h"
#include "debug.h"
#include "model/datacenter.h"

using namespace model;

/*
 * 群聊详情
 *
 */
GroupSessionDetailWidget::GroupSessionDetailWidget(const QString& chatSessionId,const QString& userId,QWidget* parent)
    :chatSessionId(chatSessionId),userId(userId),QDialog(parent)
{
    //设置基本属性
    this->setFixedSize(410,600);
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255,255,255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    //创建布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(50,20,50,50);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    //创建滚动区
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(200,200,200); }");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px;}");
    scrollArea->setFixedSize(310,350);
    scrollArea->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);


    //创建内部widgets
    QWidget* container = new QWidget();
    scrollArea->setWidget(container);

    //给container 中添加元素
    glayout = new QGridLayout();
    glayout->setSpacing(0);
    glayout->setContentsMargins(0,0,0,0);
    glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(glayout);

    //将滚动区域添加到布局管理器中
    vlayout->addWidget(scrollArea);

    //添加 "添加按钮"
    AvatarItem* addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    glayout->addWidget(addBtn,0,0);

    //添加 "群聊名称"
    QLabel* groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setStyleSheet("QLabel { font-weight: 700; font-size: 16px; }");
    groupNameTag->setFixedHeight(50);
    groupNameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
    //将群聊名称这几个字和真正的名称靠近一些
    groupNameTag->setAlignment(Qt::AlignBottom);
    vlayout->addWidget(groupNameTag);

    //添加真正的群聊名字和修改按钮
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0,0,0,0);
    vlayout->addLayout(hlayout);

    //添加真正的群里名字
    groupNameLabel = new QLabel();
    groupNameLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
    groupNameLabel->setFixedHeight(50);
    groupNameLabel->setStyleSheet("QLabel { font-size: 16px; }");
    //设置hlayout中的布局
    hlayout->addWidget(groupNameLabel,0,Qt::AlignLeft | Qt::AlignVCenter);

    //创建修改按钮
    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setFixedSize(30,30);
    modifyBtn->setIconSize(QSize(20, 20));
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(230, 230, 230); }");
    hlayout->addWidget(modifyBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    //创建退出按钮
    exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedHeight(50);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    QString style = "QPushButton { border: 1px solid rgb(90,90,90); border-radius: 5px; background-color: transparent; }";
    style += "QPushButton:pressed { background-color: rgb(230,230,230); }";
    exitGroupBtn->setStyleSheet(style);
    vlayout->addWidget(exitGroupBtn);

#if TEST_UI
    groupNameLabel->setText("测试群聊中");
    QIcon avatar(":/resource/image/avatarDefault.png");
    for (int i = 0; i < 20; ++i) {
        AvatarItem* item = new AvatarItem(avatar, "不摇碧莲" + QString::number(i));
        this->addMember(item);
    }
#endif

    // 加载群聊数据
    initData();

    // connect(exitGroupBtn, &QPushButton::clicked, this, &GroupSessionDetailWidget::clickexitGroupBtn);
}

void GroupSessionDetailWidget::addMember(AvatarItem* avatarItem)
{
    const int MAX_COW = 4;
    if(curCol >= MAX_COW) {
        ++curCow;
        curCol = 0;
    }

    glayout->addWidget(avatarItem,curCow,curCol);
    ++curCol;
}

void GroupSessionDetailWidget::initData()
{
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::getMemberListDone, this, &GroupSessionDetailWidget::initMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::initMembers(const QString& chatSessionId)
{
    DataCenter* dataCenter = DataCenter::getInstance();
    QList<UserInfo> * memberList = dataCenter->getMemberList(chatSessionId);
    if(memberList  == nullptr) {
        LOG() << "获取的成员列表为空! chatSessionId=" << chatSessionId;
        return;
    }

    for(const auto& u : *memberList) {
        AvatarItem* avatarItem = new AvatarItem(u.avatar, u.nickname);
        this->addMember(avatarItem);
    }

    groupNameLabel->setText("新的群聊");
}

/*
void GroupSessionDetailWidget::clickexitGroupBtn()
{
    auto result = QMessageBox::warning(this, "确认退出", "确认退出该群聊?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG() << "用户取消了退出群聊";
        return;
    }

    DataCenter* dataCenter = DataCenter::getInstance();
    // 连接信号槽，处理服务器响应
    // TODO 退出群聊功能
    connect(dataCenter, &DataCenter::exitGroupChatSessionDone, this,
            [=](bool success, const QString& message) {
                if (success) {
                    LOG() << "退出群聊成功：" << message;
                    QMessageBox::information(this, "成功", "成功退出群聊！");
                    this->close(); // 关闭窗口
                } else {
                    LOG() << "退出群聊失败：" << message;
                    QMessageBox::critical(this, "错误", "退出群聊失败：" + message);
                }
            });

    dataCenter->exitGroupChatSessionAsync(chatSessionId,userId);
}
*/



